// 影视相关API

import { $api } from '~/utils/request'

export interface Movie {
  id: number
  title: string
  englishTitle?: string
  poster: string
  banner?: string
  description: string
  year: number
  country: string
  language: string
  columns: any[]
  director: string
  actors: string[]
  rating: number
  duration: number
  releaseDate: string
  updateTime?: string
  status: number
  viewCount: number
  columnValue?: string
}

export interface MovieListParams {
  pageNum?: number
  pageSize?: number
  columnValue?: string
  year?: number
  country?: string
  keyword?: string
  orderBy?: 'latest' | 'hottest' | 'rating'
}

export interface VideoResource {
  id: number
  movieId: number
  episodeNumber: number
  episodeTitle: string
  resourceUrl: string
  quality: string
  duration: number
}

// 获取影视列表
export const getMovieList = (params: MovieListParams) => {
  return $api.get<{
    rows: Movie[]
    total: number
  }>('/app/movieapp/list', params)
}

// 获取影视详情
export const getMovieDetail = (id: number) => {
  return $api.get<Movie>(`/app/movieapp/${id}`)
}

// 获取视频资源列表
export const getVideoResources = (movieId: number) => {
  return $api.get<VideoResource[]>(`/app/movieapp/${movieId}/videos`)
}

// 获取播放地址
export const getPlayUrl = (videoId: number) => {
  return $api.get<VideoResource[]>(`/app/movieapp/video/${videoId}/resources`)
}

// 获取推荐影视
export const getRecommendMovies = (columnValue?: string, pageNum: number = 1, pageSize: number = 10) => {
  return $api.get<{
    rows: Movie[]
    total: number
  }>('/app/movieapp/recommend', { columnValue, pageNum, pageSize })
}

// 获取最新影视
export const getLatestMovies = (columnValue?: string, pageNum: number = 1, pageSize: number = 10) => {
  return $api.get<{
    rows: Movie[]
    total: number
  }>('/app/movieapp/new', { columnValue, pageNum, pageSize })
}

// 获取热门影视
export const getHotMovies = (columnValue?: string, pageNum: number = 1, pageSize: number = 10) => {
  return $api.get<{
    rows: Movie[]
    total: number
  }>('/app/movieapp/hot', { columnValue, pageNum, pageSize })
}

// 根据栏目获取影视列表
export const getMoviesByColumn = (columnValue: string, pageNum: number = 1, pageSize: number = 20) => {
  return $api.get<{
    rows: Movie[]
    total: number
  }>(`/app/movieapp/column/${columnValue}`, { pageNum, pageSize })
}

// 搜索影视
export const searchMovies = (keyword: string, pageNum: number = 1, pageSize: number = 20) => {
  return $api.get<{
    rows: Movie[]
    total: number
  }>('/app/movieapp/search', { keyword, pageNum, pageSize })
}

// 获取影视的演职人员
export const getMovieCasts = (movieId: number) => {
  return $api.get(`/app/movieapp/${movieId}/casts`)
}

// 获取演员详情
export const getActorDetail = (actorId: number) => {
  return $api.get(`/app/movieapp/actor/${actorId}`)
}

// 获取演员作品列表
export const getActorMovies = (actorId: number, pageNum: number = 1, pageSize: number = 20) => {
  return $api.get<{
    rows: any[]
    total: number
  }>(`/app/movieapp/actor/${actorId}/movies`, { pageNum, pageSize })
}

// 获取栏目列表
export const getColumns = (type?: string) => {
  return $api.get('/app/movieapp/columns', { type })
}

// 增加播放次数
export const increaseViewCount = (movieId: number) => {
  return $api.post(`/app/movieapp/${movieId}/play`)
}
